001    /*
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: 2002-8-22
005     * Time: 20:04:38
006     */
007    package EVolve.util.batchutils;
008    
009    import java.awt.*;
010    import java.awt.event.*;
011    import java.util.*;
012    import javax.swing.*;
013    import javax.swing.filechooser.*;
014    import java.io.*;
015    import EVolve.*;
016    import EVolve.exceptions.EVolveException;
017    import EVolve.util.batchutils.BatchInfo;
018    import EVolve.util.predefinedutils.VizInfo;
019    import EVolve.util.predefinedutils.VisualizationSerializer;
020    import EVolve.util.predefinedutils.SerializerInfo;
021    import EVolve.visualization.VizFactory.VisualizationFactory;
022    
023    public class BatchRunner {
024        private JDialog dialog;
025        private java.awt.List fileList;
026        private java.awt.List procList;
027        private Frame owner;
028        private HashSet setFile;
029        private HashSet setProcessFiles;
030        private HashMap batches;
031        private HashMap mapConfig;
032        private JTextField txtConfName;
033    
034        public BatchRunner() {
035            owner = Scene.getFrame();
036            setFile = new HashSet();
037            setProcessFiles = new HashSet();
038            batches = new HashMap();
039            mapConfig = new HashMap();
040        }
041    
042        public void showWindow() {
043            if (dialog == null) {
044                createDialog();
045                Scene.getUIManager().showDialog(dialog, dialog.getWidth(), dialog.getHeight());
046            }
047            else dialog.setVisible(true);
048        }
049    
050        public void runBatch() {
051            int iBatchNum = batches.keySet().size();
052            Iterator it = batches.keySet().iterator();
053            BatchInfo batchInfo;
054    
055            if (iBatchNum <= 0) {
056                Scene.showErrorMessage("No batch available!");
057                return;
058            }
059    
060            while (it.hasNext()) {
061                String outPath,dataFileName,batchName = (String) it.next();
062                VizInfo vizInfo = null;
063                Iterator it2;
064                batchInfo = (BatchInfo) batches.get(batchName);
065    
066                // read in configure file
067                try {
068                    it2 = batchInfo.getTraceFileList().iterator();
069                    dataFileName = (String) it2.next();
070                    Scene.setDataFilename(dataFileName);
071                    Scene.autoLoadDataSource();
072    
073                    SerializerInfo info = VisualizationSerializer.v().getVizInfoFromDisk(batchInfo.getConfigureFile());
074                    vizInfo = (VizInfo)info.vizInfoList.get(0);
075                    mapConfig.put("Factory",vizInfo.getFactory());
076                    mapConfig.put("Subject",vizInfo.getSubject());
077                    mapConfig.put("Dimension",vizInfo.getDimension());
078                    mapConfig.put("Predictor",vizInfo.getPredictor());
079                    mapConfig.put("Interval",new Integer(vizInfo.getInterval()));
080                    mapConfig.put("BeginCall",new Long(vizInfo.getBeginCall()));
081                    mapConfig.put("EndCall",new Long(vizInfo.getEndCall()));
082                    mapConfig.put("Match",new Float(vizInfo.getMatch()));
083                    mapConfig.put("Options", vizInfo.getOptions());
084                    outPath = info.resultPath;
085    
086                    execCmd(outPath,dataFileName);
087                } catch (EVolveException e) {
088                    Scene.showErrorMessage(e.getMessage());
089                    continue;
090                }
091    
092                while (it2.hasNext()) {
093                    // load data file first
094                    dataFileName = (String)it2.next();
095                    Scene.setDataFilename(dataFileName);
096                    Scene.autoLoadDataSource();
097                    execCmd(outPath,dataFileName);
098                }
099    
100                vizInfo.reset();
101                mapConfig.clear();
102            }  // end of while(it)
103            batches.clear();
104            Scene.setDataFilename(null);
105        }
106    
107        private void execCmd(String path,String dataFn) {
108            Scene.getVisualizationManager().newVisualization(((VisualizationFactory)mapConfig.get("Factory")).getName());
109    
110            Scene.getVisualizationManager().autoPreVisualize(mapConfig);
111            Scene.autoVisualize();
112            Scene.getVisualizationManager().autoSaveResult(path,dataFn);
113    
114        }
115    
116        private void createDialog(){
117            dialog = new JDialog(owner,"Create Batch",true);
118            dialog.setBounds(new Rectangle(500,400));
119    
120            JPanel batchName = new JPanel(new FlowLayout());
121            dialog.getContentPane().add(batchName,BorderLayout.NORTH);
122    
123            Box boxMain = new Box(BoxLayout.Y_AXIS);
124            boxMain.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
125                    "Choose file(s) to be processed & Processing configuration"));
126            dialog.getContentPane().add(boxMain,BorderLayout.CENTER);
127    
128    
129            Box boxLabels = new Box(BoxLayout.X_AXIS);
130            JButton buttonGetDir = new JButton("Select Directory ..");
131            buttonGetDir.addActionListener(new ActionListener(){
132                public void actionPerformed(ActionEvent e){
133                        fillFileList();
134                }
135            });
136            boxLabels.add(buttonGetDir);
137            boxLabels.add(Box.createHorizontalGlue());
138            boxLabels.add(new JLabel("Processing List                          "));
139    
140            Box boxLists = new Box(BoxLayout.X_AXIS);
141            fileList = new java.awt.List(8,true);
142    
143            Box boxAddRemove = new Box(BoxLayout.Y_AXIS);
144            JButton buttonAdd = new JButton("  >  ");
145            buttonAdd.addActionListener(new ActionListener(){
146                public void actionPerformed(ActionEvent e){
147                    addFiles();
148                }
149            });
150            JButton buttonRemove = new JButton("  <  ");
151            buttonRemove.addActionListener(new ActionListener(){
152                public void actionPerformed(ActionEvent e){
153                    removeFiles();
154                }
155            });
156            boxAddRemove.add(Box.createVerticalStrut(40));
157            boxAddRemove.add(buttonAdd);
158            boxAddRemove.add(Box.createVerticalStrut(20));
159            boxAddRemove.add(buttonRemove);
160    
161            procList = new java.awt.List(8,true);
162            boxLists.add(fileList);
163            boxLists.add(Box.createHorizontalStrut(40));
164            boxLists.add(boxAddRemove);
165            boxLists.add(procList);
166    
167            boxMain.add(boxLabels);
168            boxMain.add(Box.createVerticalStrut(5));
169            boxMain.add(boxLists);
170    
171            Box boxConfig = new Box(BoxLayout.X_AXIS);
172            boxConfig.add(new JLabel("Choose configure file:"));
173            txtConfName = new JTextField(12);
174            boxConfig.add(Box.createHorizontalStrut(20));
175            boxConfig.add(txtConfName,BorderLayout.CENTER);
176            JButton buttonConfig = new JButton("...");
177            buttonConfig.addActionListener(new ActionListener(){
178                public void actionPerformed(ActionEvent e){
179                    txtConfName.setText(chooseConfig());
180                }
181            });
182            boxConfig.add(buttonConfig,BorderLayout.EAST);
183    
184            Box boxOkCancel = new Box(BoxLayout.X_AXIS);
185            JButton buttonOK = new JButton("OK");
186            JButton buttonCancel = new JButton("Cancel");
187    
188            buttonOK.addActionListener(new ActionListener(){
189                public void actionPerformed(ActionEvent e){
190                    onOK();
191                }
192            });
193            buttonCancel.addActionListener(new ActionListener(){
194                public void actionPerformed(ActionEvent e){
195                    onCancel();
196                }
197            });
198            boxOkCancel.add(Box.createHorizontalStrut(30));
199            boxOkCancel.add(buttonOK);
200            boxOkCancel.add(Box.createHorizontalStrut(20));
201            boxOkCancel.add(buttonCancel);
202    
203            Box boxBottom = Box.createVerticalBox();
204            boxBottom.add(Box.createVerticalStrut(12));
205            boxBottom.add(boxConfig);
206            boxBottom.add(Box.createVerticalStrut(40));
207            boxBottom.add(boxOkCancel);
208    
209    
210            dialog.getContentPane().add(boxBottom,BorderLayout.SOUTH);
211            //dialog.setResizable(false);
212        }
213    
214        private void clearList() {
215            fileList.removeAll();
216            procList.removeAll();
217            setFile.clear();
218            setProcessFiles.clear();
219        }
220    
221        private void onOK() {
222            BatchInfo batchInfo = new BatchInfo();
223    
224            if (txtConfName.getText().trim().length() == 0) {
225                Scene.showErrorMessage("No configuration file selected!");
226                return;
227            }
228    
229            if (setProcessFiles.size() == 0) {
230                Scene.showErrorMessage("Select file(s) to be processed please!");
231                return;
232            }
233    
234            batchInfo.setConfigureFile(txtConfName.getText().trim());
235            batchInfo.setTraceFileList(setProcessFiles);
236            batches.put("Batch "+batches.entrySet().size(),batchInfo);
237            dialog.setVisible(false);
238            clearList();
239        }
240    
241        private void onCancel() {
242            dialog.setVisible(false);
243            clearList();
244        }
245    
246        private String chooseConfig() {
247            JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastConfigDir());
248    
249            if(fc.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) {
250                File f = fc.getSelectedFile();
251                Scene.getUIManager().setLastConfigDir(f.getPath());
252                return (f.getPath());
253            }
254            else return "";
255        }
256    
257        private void addFiles() {
258            String[] selItems = fileList.getSelectedItems();
259    
260            for (int i=0;i<selItems.length;i++) {
261                if (setProcessFiles.contains(selItems[i])) continue;
262    
263                setProcessFiles.add(selItems[i]);
264                procList.add(selItems[i]);
265            }
266        }
267    
268        private void removeFiles() {
269            String[] selItems = procList.getSelectedItems();
270    
271            for (int i=0;i<selItems.length;i++) {
272                setProcessFiles.remove(selItems[i]);
273                procList.remove(selItems[i]);
274            }
275        }
276    
277        private void fillFileList() {
278            JFileChooser fc = new JFileChooser();
279            String path;
280    
281            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
282    
283            if(fc.showOpenDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) {
284                path = fc.getSelectedFile().getAbsolutePath();
285                File dir = new File(path);
286                FileSystemView fv = fc.getFileSystemView();
287                File[] fl = fv.getFiles(dir,false);
288                for (int i=0;i<fl.length;i++) {
289                    String fn = fl[i].getName();
290                    if (!fl[i].isFile() || !fn.endsWith(".dat") || setFile.contains(path+File.separator+fn))
291                        continue;
292                    setFile.add(path+File.separator+fn);
293                    fileList.add(path+File.separator+fn);
294                }
295            }
296    
297        }
298    
299    }